package edu.scau.controller;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;

import edu.scau.model.Department;
import edu.scau.model.Doctor;
import edu.scau.model.Drug;
import edu.scau.model.Item;
import edu.scau.model.Patient;
import edu.scau.model.Pcheck;
import edu.scau.model.Prescription;
import edu.scau.model.Ward;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;

public class MainController extends Controller {
	public void index() {
		setAttr("department", Department.dao.findAll());
		setAttr("drug", Drug.dao.findAll());
		setAttr("ward", Ward.dao.findAll());
		setAttr("item", Item.dao.findAll());
		render("/views/main.html");
	}

	public void roominfo() {
		String room = getPara("id");
		renderJson("data", Ward.dao.findByRoomNo(room));
	}

	public void patientAll() {
		String m_id = getPara("p_id");
		renderJson("p_data", Patient.dao.getPatientView(m_id));
	}

	public void ruyuan() {
		String name = getPara("name");
		renderJson("data", Ward.dao.findEnptyWardByName(name));
	}

	public void newpaient() {
		String json = getPara("info");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			JSONObject jo = JSONObject.parseObject(json);
			Patient patient = new Patient();
			patient.setPId(Patient.dao.getNewPID());
			patient.setMId(Patient.dao.getNewMID());
			patient.setPName(jo.getString("name"));
			patient.setSex(jo.getString("sex"));
			patient.setDiagnosis(jo.getString("dia"));
			patient.setDId(jo.getString("doc"));
			patient.setWId(jo.getString("wr"));
			patient.setBedId(jo.getString("bd"));
			patient.save();
			map.put("mid", patient.getMId());
			map.put("success", true);
		} catch (Exception e) {
			map.put("success", false);
			map.put("fail", e.getMessage());
		}
		renderJson("success", map);
	}

	public void getinfobyblh() {
		Integer blh = getParaToInt("blh");
		Patient p = Patient.dao.getPatientByblh(blh);
		renderJson("patient", p);
	}

	public void updatePatient() {
		String json = getPara("info");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			JSONObject jo = JSONObject.parseObject(json);
			Patient patient = new Patient();
			patient.setPId(Patient.dao.getNewPID());
			patient.setMId(jo.getInteger("id"));
			patient.setPName(jo.getString("name"));
			patient.setSex(jo.getString("sex"));
			patient.setDiagnosis(jo.getString("dia"));
			patient.setDId(jo.getString("doc"));
			patient.setWId(jo.getString("wr"));
			patient.setBedId(jo.getString("bd"));
			patient.save();
			map.put("mid", patient.getMId());
			map.put("success", true);
		} catch (Exception e) {
			map.put("success", false);
			map.put("fail", e.getMessage());
		}
		renderJson("success", map);
	}

	public void kaiyao() {
		String json = getPara("druglist");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			JSONArray ja = JSONArray.parseArray(json);
			int drugno = Prescription.dao.getNewNO();
			for (int i = 0; i < ja.size(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				Prescription prescription = new Prescription();
				prescription.setPId(jo.getInteger("patient"));
				prescription.setDrugName(jo.getString("name"));
				prescription.setDrugNo(drugno);
				prescription.setDId(jo.getString("docid"));
				prescription.setDrugCount(jo.getInteger("num"));
				prescription.save();
			}
			map.put("success", true);
		} catch (Exception e) {
			System.out.println(e);
			map.put("success", false);
			map.put("fail", e.getMessage());
		}
		renderJson("success", map);
	}

	public void jiancha() {
		String json = getPara("itemlist");
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			JSONArray ja = JSONArray.parseArray(json);
			for (int i = 0; i < ja.size(); i++) {
				JSONObject jo = ja.getJSONObject(i);
				Pcheck pcheck = new Pcheck();
				pcheck.setDId(jo.getString("docid"));
				pcheck.setItemName(jo.getString("name"));
				pcheck.setPId(jo.getInteger("patient"));
				pcheck.save();
			}
			map.put("success", true);
		} catch (Exception e) {
			System.out.println(e);
			map.put("success", false);
			map.put("fail", e.getMessage());
		}
		renderJson("success", map);
	}

	public void chuyuancheck() {
		Integer pid = getParaToInt("pid");
		renderJson("info", Patient.dao.getPatientInfo(pid));
	}

	public void chuyuansure() {
		Integer pid = getParaToInt("pid");
		Map<String, Object> map = new HashMap<String, Object>();
		Patient patient = Patient.dao.findById(pid);
		try {
			patient.setFlag("1");
			patient.setOutTime(new Date(new java.util.Date().getTime()));
			patient.update();
			map.put("success", true);
		} catch (Exception e) {
			map.put("fail", e.getMessage());
		}
		renderJson("data", map);
	}

	public void suanfeiyong() {
		Integer pid = getParaToInt("pid");
		Object object = Doctor.dao.getPrice(pid);
		renderJson("cost",object);
	}
}
